import { _decorator, Component, Node, Sprite, Button, Label, Vec2, v2, tween, Tween, SpriteFrame, randomRangeInt, AudioClip, randomRange, Vec3, v3, UIOpacity } from 'cc';
const { ccclass, property } = _decorator;

import { SSS_Consts } from "../../../../Script/Data/SSS_Consts";
import { YangLeGeYang_CardGameState } from "../Data/YangLeGeYang_GameEnum";
import { WordList } from "../../../../Script/Data/WordList";
import YangLeGeYang_GameFunction from "../Manager/YangLeGeYang_GameFunction";
import { bdlMgr } from "../../../../Script/Manager/BundleManager_S";
import CommonFunction from "../../../../Script/Utils/CommonFunction";
import { YangLeGeYang_GameData } from "../Data/YangLeGeYang_GameData";
import YangLeGeYang_CardManager from "../Manager/YangLeGeYang_CardManager";
import YangLeGeYang_GameManager from "../Manager/YangLeGeYang_GameManager";
import YangLeGeYang_OperatePanel from "../UI/YangLeGeYang_OperatePanel";
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';

// /**卡片 */
@ccclass('YangLeGeYangCard')
export default class YangLeGeYang_Card extends Component {
        @property({ type: Node })
        public main: Node | null = null;
        @property({ type: Sprite })
        public Sprite_bg: Sprite | null = null;
        @property({ type: Sprite })
        public Sprite_icon: Sprite | null = null;
        @property({ type: Node })
        public cover: Node | null = null;
        @property({ type: Button })
        public Button_click: Button | null = null;
        //    // @property({ type: Label })
        //    // public Label_layer: Label = null;
        @property({ type: Label, tooltip: "Text_Chinese" })
        Text_Chinese: Label | null = null;
        @property({ type: Label, tooltip: "Text_English" })
        Text_English: Label | null = null;
        //    /**类型 */
        card_type;
        //    /**层 */
        card_layer: number = 1;
        //    /**位置 */
        card_pos: Vec2 = v2(0, 0);
        //    /**数组位置 */
        list_index: number = 0;
        isCover: boolean = false;
        show_type: number = 0;
        onLoad() {

        }
        Init(_card_type) {
                this.card_type = _card_type;

                let random100 = randomRangeInt(0, 100);
                let arr = [0, 74, 95, 100];
                if (random100 >= arr[0] && random100 < arr[1]) {
                        this.show_type = 2;
                }
                else if (random100 >= arr[1] && random100 < arr[2]) {
                        this.show_type = 0;
                }
                else {
                        this.show_type = 1;
                }

                this.show_type = 2;

                // console.log(`this.show_type`, this.show_type);

                this.AddListener();
                this.Refresh();
                this.RefreshIcon();
        }
        AddListener() {
                // this.Button_click.node.on(Node.EventType.TOUCH_END, this.On_MOUSE_CLICK, this);
                this.Button_click.node.on(Node.EventType.TOUCH_START, this.On_MOUSE_CLICK, this);
                // this.Button_click.node.on(Node.EventType.TOUCH_START, this.On_TOUCH_START, this);
                this.Button_click.node.on(Node.EventType.TOUCH_CANCEL, this.On_TOUCH_CANCEL, this);
        }
        Refresh() {
                this.cover.active = this.isCover;
                this.Button_click.interactable = !this.isCover;

                const isShowFullCover = YangLeGeYang_GameData.MapData_CurrentLevel[this.list_index].isShowFullCover;
                this.Sprite_icon.node.active = !(this.isCover && isShowFullCover);

                // this.Label_layer.string = this.card_layer.toString();
        }
        async RefreshIcon() {
                // const item_data = WordList[this.card_type];
                const item_data = WordList.find(element => element.key === this.card_type);

                // console.log(`card_type`, this.card_type);
                // console.log(`RefreshIcon-item_data----`, item_data);

                this.Text_English.node.active = false;
                this.Text_Chinese.node.active = false;
                this.Sprite_icon.node.active = false;

                if (this.show_type === 0) {
                        this.Text_English.node.active = true;
                        // this.Text_English.string = item_data.word.en;
                        this.Text_English.string = item_data.word.en_show;
                }
                else if (this.show_type === 1) {
                        this.Text_Chinese.node.active = true;
                        this.Text_Chinese.string = item_data.word.zh;
                }
                else {
                        this.Sprite_icon.node.active = true;


                        // console.log(`this.card_type`, this.card_type)
                        // bdlMgr.getRes<SpriteFrame>(SSS_Consts.BundleName.SPRITE, `icon/${DanLeGeCi_GameData.CurrentLevelData.root}/${item_data.word.sprite}`, SpriteFrame).then(res => {
                        // bdlMgr.getRes<SpriteFrame>(`YangLeGeYang_Sprite`, `icon/西瓜/${this.card_type}`, SpriteFrame).then(res => {
                        bdlMgr.getRes<SpriteFrame>(`YangLeGeYang_Sprite`, `icon_e/${YangLeGeYang_GameData.CurrentLevelData.root}/${this.card_type}/spriteFrame`, SpriteFrame).then(res => {
                                if (this.Sprite_icon) {
                                        this.Sprite_icon.spriteFrame = res;

                                        this.Sprite_icon.sizeMode = Sprite.SizeMode.TRIMMED;
                                        // this.Sprite_icon.node.scale = 1

                                        CommonFunction.SetFrame(this.Sprite_icon, 66, 66);
                                }
                        });
                }
        }
        On_TOUCH_START(t) {
                // this.node.zIndex = 999;
                // CommonFunction.SetLastSiblingIndex(this.node);
        }
        On_TOUCH_CANCEL(t) {
                this.node.zIndex = 0;
                // CommonFunction.SetLastSiblingIndex(this.node);
        }
        On_MOUSE_CLICK(t) {
                this.node.zIndex = 999;

                // console.log(`卡牌`);
                // TipPanel.Instance.Show(`卡牌`);

                if (YangLeGeYang_GameData.isDead) {
                        return;
                }

                let cardGameState = YangLeGeYang_GameData.MapData_CurrentLevel[this.list_index].cardGameState;
                let isCanClick: boolean = false;
                if ((cardGameState === YangLeGeYang_CardGameState.游戏场景 || cardGameState === YangLeGeYang_CardGameState.空地) && !this.isCover) {
                        isCanClick = true;
                }
                if (isCanClick) {
                        // const itemdata_CardCommonData = CardCommonData[this.card_type];
                        // const card_name: string = itemdata_CardCommonData.card_name;
                        // console.log(this.card_type);
                        // console.log(this.card_type, card_name);


                        let target = this.main;
                        Tween.stopAllByTarget(target);
                        // target.angle = 30;
                        const duration = 0.2;

                        const stgOg_angle = 0;
                        const duration_2 = 0.05;
                        let 幅度 = 1;
                        let 幅度_scale = 15;

                        // target.angle = 20
                        // let tweener_1 = tween(target)
                        //     .to(0.05, { angle: randomRange(-30, 30) })
                        //     .to(duration, { angle: 0 })
                        //     .to(duration_2, { angle: stgOg_angle + -幅度 * 幅度_scale })
                        //     .to(duration_2, { angle: stgOg_angle })
                        //     .to(duration_2, { angle: stgOg_angle + +幅度 * 幅度_scale })
                        //     .to(duration_2, { angle: stgOg_angle })
                        //     .start();

                        // return


                        // this.点击晃动();
                        // let 拖尾 = this.node.getComponent(MotionTrail);
                        // 拖尾.length = 10;

                        YangLeGeYang_GameData.LastClickCardIndex = this.list_index;
                        this.ToOperatePanel();


                        // GameFunction.RefreshAllCardCover(DanLeGeCi_GameData.MapData_CurrentLevel);
                        const target_item_MapData = YangLeGeYang_GameData.MapData_CurrentLevel[this.list_index];
                        const card_layer = target_item_MapData.card_layer;
                        // const x = target_item_MapData.card_pos.x;
                        // const y = target_item_MapData.card_pos.y;
                        YangLeGeYang_GameFunction.Refresh9CardCover(YangLeGeYang_GameData.MapData_CurrentLevel, this.list_index, card_layer, target_item_MapData.card_pos);

                        let audio_root = `audio/tile click`;
                        // let arr = [`tile click`, `tile click blocked`]
                        // let random_index = randomRangeInt(0, arr.length);
                        // let name = arr[random_index];
                        // let name = `tile click`;
                        // let audio_root = `audio/${name}`;
                        bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
                                SSS_AudioManager.Instance.playSFX(res, false);
                        });



                        let item = YangLeGeYang_GameData.MapData_EmptyLand.find(i => i.list_index == this.list_index)
                        // console.log(`item`,item);

                        let index = YangLeGeYang_GameData.MapData_EmptyLand.indexOf(item);
                        if (index != -1) {
                                YangLeGeYang_GameData.MapData_EmptyLand.splice(index, 1);
                        }
                        YangLeGeYang_GameFunction.RefreshEmptyLand();


                        // AudioManager.Play_sound("click", false, 1);

                        // if (GameStorage.data.vibrate_on) {
                        //     CommonFunction.VibrateShort();
                        // }
                }
                else {
                        console.log(`挡着了`);
                        this.node.zIndex = 0;

                        this.挡着了晃动();
                }
        }
        点击晃动() {
                let target = this.main;
                Tween.stopAllByTarget(target);
                target.angle = 0;

                const stgOg_angle = 0;
                const duration = 0.05;
                let 幅度 = 1;
                let 幅度_scale = 20;
                let tweener_1 = tween(target)
                        .to(duration, { angle: stgOg_angle + -幅度 * 幅度_scale })
                        .to(duration, { angle: stgOg_angle + +幅度 * 幅度_scale })
                        // .to(duration, { angle: stgOg_angle + randomRange(-幅度, 幅度) * 幅度_scale })
                        // .to(duration, { angle: stgOg_angle + randomRange(-幅度, 幅度) * 幅度_scale })
                        .to(duration, { angle: stgOg_angle })
                        .union()
                        .repeat(2)
                        .start();

                let name = `tile click blocked`;
                let audio_root = `audio/${name}`;
                bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
                        SSS_AudioManager.Instance.playSFX(res, false);
                });
        }
        挡着了晃动() {
                let target = this.main;
                Tween.stopAllByTarget(target);
                // target.x = 0;
                // target.y = 0;
                target.position = v3(0, 0);

                const stgOgY = 0;
                const stgOgX = 0;
                const duration = 0.014;
                let 幅度 = 1;
                let 幅度_scale = 4;
                let tweener_1 = tween(target)
                        // .to(duration, { y: stgOgY + randomRange(-幅度, 幅度) * 幅度_scale, x: stgOgX + randomRange(-幅度, 幅度) * 幅度_scale })
                        // .to(duration, { y: stgOgY + randomRange(-幅度, 幅度) * 幅度_scale, x: stgOgX + randomRange(-幅度, 幅度) * 幅度_scale })
                        // .to(duration, { y: stgOgY + randomRange(-幅度, 幅度) * 幅度_scale, x: stgOgX + randomRange(-幅度, 幅度) * 幅度_scale })
                        // .to(duration, { y: stgOgY, x: stgOgX })

                        .to(duration, {
                                position: new Vec3(
                                        stgOgX + randomRange(-幅度, 幅度) * 幅度_scale,
                                        stgOgY + randomRange(-幅度, 幅度) * 幅度_scale,
                                        0
                                )
                        })
                        .to(duration, {
                                position: new Vec3(
                                        stgOgX + randomRange(-幅度, 幅度) * 幅度_scale,
                                        stgOgY + randomRange(-幅度, 幅度) * 幅度_scale,
                                        0
                                )
                        })
                        .to(duration, {
                                position: new Vec3(
                                        stgOgX + randomRange(-幅度, 幅度) * 幅度_scale,
                                        stgOgY + randomRange(-幅度, 幅度) * 幅度_scale, 0)
                        }).to(duration, { position: new Vec3(stgOgX, stgOgY, 0) })


                        .union()
                        .repeat(3)
                        .start();

                let name = `tile click blocked`;
                let audio_root = `audio/${name}`;
                bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, audio_root, AudioClip).then(res => {
                        SSS_AudioManager.Instance.playSFX(res, false);
                });
        }
        AddCardToOperatePanel() {
                // DanLeGeCi_GameData.MapData_CurrentLevel[this.list_index].isOperate = true;
                const target_item_MapData = YangLeGeYang_GameData.MapData_CurrentLevel[this.list_index];
                // console.log(`target_item_MapData`, target_item_MapData);

                let isAdd: boolean = false;
                for (let index = 0; index < YangLeGeYang_GameData.MapData_Operate.length; index++) {
                        const element = YangLeGeYang_GameData.MapData_Operate[index];
                        // console.log(`element`, element);
                        if (target_item_MapData.card_type === element.card_type) {
                                YangLeGeYang_GameData.MapData_Operate.splice(index + 1, 0, target_item_MapData);
                                isAdd = true;
                                break;
                        }
                }
                if (isAdd === false) {
                        YangLeGeYang_GameData.MapData_Operate.push(target_item_MapData);
                }

                let isDead = YangLeGeYang_CardManager.IsDead();
                if (isDead) {
                        // TipPanel.Instance.Show(`死亡`);
                        YangLeGeYang_GameData.isDead = true;

                        YangLeGeYang_GameManager.Instance.GameFail();

                        // if (DanLeGeCi_GameData.LevelRebornNumber > 0) {
                        //     RebornPanel.Instance.ShowOn();
                        // }
                        // else {
                        //     GameManager.Instance.GameFail();
                        // }
                }

                // console.log(`AddCardToOperatePanel---RefreshAllCard---`);
                YangLeGeYang_OperatePanel.Instance.RefreshAllCard();
        }
        ToOperatePanel() {
                // console.log(`ToOperatePanel`);
                YangLeGeYang_GameData.MapData_CurrentLevel[this.list_index].cardGameState = YangLeGeYang_CardGameState.操作台;
                this.AddCardToOperatePanel();
                // this.XiaoChu3();
        }
        ToEmptyLand() {
                YangLeGeYang_GameData.MapData_CurrentLevel[this.list_index].cardGameState = YangLeGeYang_CardGameState.空地;
        }
        ToGameLand() {
                YangLeGeYang_GameData.MapData_CurrentLevel[this.list_index].cardGameState = YangLeGeYang_CardGameState.游戏场景;
        }
        ToDelete() {
                YangLeGeYang_GameData.MapData_CurrentLevel[this.list_index].cardGameState = YangLeGeYang_CardGameState.消除;
                // console.log(this.list_index, DanLeGeCi_GameData.MapData_CurrentLevel[this.list_index].cardGameState);

                let uiOpacity = this.node.getComponent(UIOpacity);
                tween(this.node)
                        // .to(0.25, { scale: 0.00, opacity: 0 }, { easing: '' })
                        .to(0.25, { scale: v3(0, 0, 0) })
                        .call(() => {
                                this.node.scale = v3(1, 1, 1);
                                uiOpacity.opacity = 255;
                                this.node.active = false;
                        })
                        .start()

                tween(uiOpacity)
                        .to(0.25, { opacity: 0 })
                        .start()
        }
}